<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-title">
        <img src="@/assets/imgs/logo.png" alt="" class="logo">
        <h2>党团信息管理系统</h2>
        <p class="subtitle">用户注册</p>
      </div>

      <el-form :model="data.form" ref="formRef" :rules="data.rules" class="register-form">
        <el-form-item prop="username">
          <el-input
              :prefix-icon="User"
              size="large"
              v-model="data.form.username"
              placeholder="请输入账号"
              class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              :prefix-icon="Lock"
              size="large"
              v-model="data.form.password"
              placeholder="请输入密码"
              show-password
              class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
              :prefix-icon="Lock"
              size="large"
              v-model="data.form.confirmPassword"
              placeholder="请确认密码"
              show-password
              class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="realName">
          <el-input
              :prefix-icon="User"
              size="large"
              v-model="data.form.realName"
              placeholder="请输入真实姓名"
              class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
              :prefix-icon="Iphone"
              size="large"
              v-model="data.form.phone"
              placeholder="请输入手机号"
              class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="email">
          <el-input
              :prefix-icon="Message"
              size="large"
              v-model="data.form.email"
              placeholder="请输入邮箱"
              class="custom-input"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              size="large"
              type="primary"
              class="register-button"
              @click="register"
          >立即注册</el-button>
        </el-form-item>
      </el-form>

      <div class="login-link">
        已有账号？<a href="/login">立即登录</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { User, Lock, Message, Iphone } from "@element-plus/icons-vue";
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import router from "@/router";

const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请确认密码'))
  } else if (value !== data.form.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const data = reactive({
  form: {
    username: '',
    password: '',
    confirmPassword: '',
    realName: '',
    phone: '',
    email: '',
    userType: 5  // 默认学生
  },
  rules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 3, max: 20, message: '账号长度在3到20个字符之间', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度在6到20个字符之间', trigger: 'blur' }
    ],
    confirmPassword: [
      { validator: validatePass, trigger: 'blur' }
    ],
    realName: [
      { required: true, message: '请输入真实姓名', trigger: 'blur' }
    ],
    phone: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
    ],
    email: [
      { required: true, message: '请输入邮箱', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
    ]
  }
})

const formRef = ref()

// 点击注册按钮的时候会触发这个方法
const register = () => {
  formRef.value.validate((valid => {
    if (valid) {
      // 调用后台的接口
      request.post('/sysUser/register', data.form).then(res => {
        if (res.code === '200') {
          ElMessage.success("注册成功")
          router.push('/login')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })).catch(error => {
    console.error(error)
  })
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #c8161d 0%, #e54b4b 100%);
  position: relative;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('@/assets/imgs/pattern.png') repeat;
  opacity: 0.1;
}

.register-box {
  width: 450px;
  padding: 40px;
  border-radius: 12px;
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  position: relative;
}

.register-title {
  text-align: center;
  margin-bottom: 40px;
}

.register-title .logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
}

.register-title h2 {
  font-size: 24px;
  color: #c8161d;
  margin: 0;
  font-weight: bold;
  letter-spacing: 2px;
}

.register-title .subtitle {
  font-size: 16px;
  color: #666;
  margin-top: 8px;
}

.register-form {
  margin-top: 20px;
}

.custom-input :deep(.el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border-radius: 8px;
  padding: 8px 15px;
}

.custom-input :deep(.el-input__inner) {
  height: 42px;
}

.register-button {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  background: linear-gradient(90deg, #c8161d, #e54b4b);
  border: none;
  transition: all 0.3s ease;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(200, 22, 29, 0.3);
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.login-link a {
  color: #c8161d;
  text-decoration: none;
  font-weight: 500;
  margin-left: 4px;
}

.login-link a:hover {
  text-decoration: underline;
}

/* 输入框focus状态的样式 */
:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #c8161d !important;
}

/* 表单验证动画 */
.el-form-item.is-error :deep(.el-input__wrapper) {
  animation: shake 0.5s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* 输入框图标样式 */
:deep(.el-input__prefix-icon) {
  font-size: 18px;
  color: #909399;
}

/* 表单项间距 */
:deep(.el-form-item) {
  margin-bottom: 20px;
}

/* 响应式适配 */
@media screen and (max-width: 480px) {
  .register-box {
    width: 90%;
    padding: 30px 20px;
  }

  .register-title h2 {
    font-size: 20px;
  }
}
</style>

